package com.puge.eduservice.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.puge.eduservice.entity.EduCourse;
import com.puge.eduservice.entity.EduCourseDescription;
import com.puge.eduservice.entity.vo.CourseInfoVo;
import com.puge.eduservice.entity.vo.CoursePublicVo;
import com.puge.eduservice.mapper.EduCourseMapper;
import com.puge.eduservice.service.EduChapterService;
import com.puge.eduservice.service.EduCourseDescriptionService;
import com.puge.eduservice.service.EduCourseService;
import com.puge.eduservice.service.EduVideoService;
import com.puge.exceptionhandle.PuGeException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-06-14
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

//    注入入EduCourseDescriptionService(课程描述)类：调用EduCourseDescriptionService类实例化对象，来调用他内部的方法
    @Autowired
    private EduCourseDescriptionService courseDescriptionService;

    //  注入小节和章节service
    @Autowired
    private EduVideoService eduVideoService;

    @Autowired
    private EduChapterService eduChapterService;

    /** 添加课程的基本信息的方法
     * @return*/
    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {

//        1,向课程表添加课程基本信息
//        CourseInfoVo 对象转换 eduCourse  对象
        EduCourse eduCourse = new EduCourse();
//        System.out.println("courseInfoVo的值："+courseInfoVo);
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
//        System.out.println("eduCourse的值:"+eduCourse);
//
        int insert = baseMapper.insert(eduCourse);
        if (insert == 0) {
//            添加失败，提示信息
            throw new PuGeException(20001,"添加课程信息失败");
        }
//        2,向课程简介表添加课程简介

        //获取添加之后课程id
        String cid = eduCourse.getId();

        EduCourseDescription courseDescription = new EduCourseDescription();
        courseDescription.setDescription(courseInfoVo.getDescription());

        courseDescription.setId(cid);
        courseDescriptionService.save(courseDescription);

        return cid;
    }
//根据课程Id查询课程基本信息
    @Override
    public CourseInfoVo getCourseInfo(String courseId) {
//       1，查询课程表
        EduCourse eduCourse = baseMapper.selectById(courseId);  //将根据id查询出来的课程，封装到 eduCourse 的对象中去
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties(eduCourse,courseInfoVo);

//       2，查询描述表
        EduCourseDescription courseDescription = courseDescriptionService.getById(courseId);
        courseInfoVo.setDescription(courseDescription.getDescription());
        return courseInfoVo;
    }

//    根据Id修改课程信息
    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
//         1，修改课程表
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int update = baseMapper.updateById(eduCourse);
        if (update == 0){
            throw new PuGeException(20001,"修改课程信息失败");

        }
//        2，修改描述表
        EduCourseDescription description = new EduCourseDescription();
        description.setId(courseInfoVo.getId());
        description.setDescription(courseInfoVo.getDescription());
        courseDescriptionService.updateById(description);

    }
    
//    根据课程Id查询课程确认信息
    @Override
    public CoursePublicVo publishCourseInfo(String id) {
//         调用mapper
        CoursePublicVo publishCourseInfo = baseMapper.getPublishCourseInfo(id);

        return publishCourseInfo;
    }
//    删除课程：需要将课程内的内容从小到大，从里到内，依次删除
    @Override
    public void removeCourse(String courseId) {
    //     1,根据课程Id删除小节
        eduVideoService.removeVideoByCourseId(courseId);
    //    2，根据课程Id删除章节
        eduChapterService.removeChapterByCourseId(courseId);

    //     3，根据课程id删除描述
        courseDescriptionService.removeById(courseId);

    //     4，根据课程id删除课程本身
        int result =  baseMapper.deleteById(courseId);

        if (result == 0){   //失败返回提醒
            throw new PuGeException(20001,"删除失败");
        }
    }
}





























